package com.example.sunmiv1.dialog

import android.app.Activity
import android.graphics.Color
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.PopupWindow
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.example.sunmiv1.R
import com.example.sunmiv1.listener.AdapterItemClickListener
import com.noober.background.drawable.DrawableCreator
import com.tencent.bugly.proguard.f
import com.zhy.autolayout.utils.AutoUtils
import kotlinx.android.synthetic.main.dialog_silverdiscount.view.rv_view
import kotlinx.android.synthetic.main.item_silverdiscount.view.item_line
import kotlinx.android.synthetic.main.item_silverdiscount.view.item_tv


class SilverDiscountDialog: PopupWindow {
    private lateinit var mActivity:Activity
    private val mView by lazy {
        LayoutInflater.from(mActivity).inflate(R.layout.dialog_silverdiscount,null)
    }
    private val mList by lazy {
        arrayListOf<String>()
    }
    private val TAG=javaClass.simpleName
    private var mListener:AdapterItemClickListener<Double>?=null
    constructor(activity: Activity,listener: AdapterItemClickListener<Double>,width:Int):super(){
        Log.e(TAG, "init width=$width")
        mActivity=activity
        contentView=mView
        mListener=listener
        setWidth(width)
        height=AutoUtils.getPercentHeightSize(240)
        isFocusable=true
        isOutsideTouchable=true
        setBackgroundDrawable(DrawableCreator.Builder().also {
            it.setSolidColor(Color.WHITE)
            it.setCornersRadius(AutoUtils.getPercentHeightSize(6).toFloat())
        }.build())
    }

    override fun showAsDropDown(anchor: View?, xoff: Int, yoff: Int, gravity: Int) {
        super.showAsDropDown(anchor, xoff, yoff, gravity)
        val adapter=MyAdapter()
        mList.clear()
        var start=100
        (0 until 20).forEach {
            mList.add(start.toString())
            start-=5
            if(start<=0){
                return@forEach
            }
        }
        Log.e(TAG, "showAtLocation: list=$mList" )
        mView.apply {
            rv_view.layoutManager=LinearLayoutManager(mActivity)
            rv_view.adapter=adapter
        }
        val lp: WindowManager.LayoutParams = mActivity.getWindow().getAttributes()
        lp.alpha = 0.75f
        mActivity.getWindow().setAttributes(lp)
    }
    fun showView(parent: View?){
        showAsDropDown(parent,0,2,Gravity.NO_GRAVITY)
    }
    private inner class MyAdapter:Adapter<MyAdapter.MyHolder>(){
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
            val view=LayoutInflater.from(mActivity).inflate(R.layout.item_silverdiscount,parent,false)
            view.layoutParams=ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)
            return MyHolder(view)
        }

        override fun getItemCount(): Int {
            return mList.size
        }

        override fun onBindViewHolder(holder: MyHolder, position: Int) {
            holder.itemView.apply {
                item_tv.text=mList[position]+"折"
                setOnClickListener {
                    mListener?.onItemClick(holder.adapterPosition,mList[position].toDouble())
                    dismiss()
                }
                item_line.visibility=if(position+1<itemCount){
                    View.VISIBLE
                }else{
                    View.GONE
                }
            }
        }
        private inner class MyHolder(view:View):ViewHolder(view){

        }
    }

    override fun dismiss() {
        val lp: WindowManager.LayoutParams = mActivity.getWindow().getAttributes()
        lp.alpha = 1f
        mActivity.getWindow().setAttributes(lp)
        super.dismiss()
    }
}